package com.ltu.m7011e.imagine.client.ui;

import com.google.gwt.dom.client.Document;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Class to create a <ul> list with widgets.
 * 
 * @author Tim Kornhammar
 */
public class MenuPanel extends ComplexPanel
{
	private Element list;
	
	public MenuPanel()
	{
		list = Document.get().createULElement().cast();
		setElement(list);
	}
	
	protected Element getList()
	{
		return list;
	}
	
	Element getWidgetTd(Widget w)
	{
		if(w.getParent() != this)
		{
			return null;
		}
		
		return DOM.getParent(w.getElement());
	}
	
	@Override
	public void add(Widget w)
	{
		Element li = Document.get().createLIElement().cast();
		DOM.appendChild(list, li);
		super.add(w, li);
	}
	
	public void insert(Widget w, int beforeIndex)
	{
		checkIndexBoundsForInsertion(beforeIndex);

		Element li = Document.get().createLIElement().cast();
		DOM.insertChild(list, li, beforeIndex);
		super.insert(w, li, beforeIndex, false);
	}
	
	@Override
	public boolean remove(Widget w)
	{
		Element li = DOM.getParent(w.getElement());
		boolean removed = super.remove(w);
		if(removed)
		{
			DOM.removeChild(list, li);
		}
		return removed;
	}
}
